inherit CometCompo_evolution_CFC CommonFC

#___________________________________________________________________________________________________________________________________________
method CometCompo_evolution_CFC constructor {} {
    set this(L_outputs)      [list]
    set this(L_inputs)       [list]
    set this(L_param)        [list]
    set this(Core)           [list]
    set this(Ext_input)      0
    set this(Ext_output)     0
    set this(has_processed)  0
    set this(can_process)    0
    set this(has_error)      0
	
	set this(nb_max_process) *
	set this(nb_process)     0

	set this(core_f_name)    ""
	set this(serialization)  ""
	
	set this(C_Magellan)     ""
	
 return $objName
}

#___________________________________________________________________________________________________________________________________________
Generate_accessors CometCompo_evolution_CFC [list core_f_name Core Ext_input Ext_output has_processed can_process has_error nb_max_process nb_process C_Magellan]
Generate_List_accessor CometCompo_evolution_CFC L_outputs L_outputs
Generate_List_accessor CometCompo_evolution_CFC L_inputs L_inputs
Generate_List_accessor CometCompo_evolution_CFC L_param L_param

#___________________________________________________________________________________________________________________________________________
method CometCompo_evolution_CFC get_comet_root_to_be_transformed {} {
 return [$this(C_Magellan) get_comet_root_to_be_transformed]
}

#___________________________________________________________________________________________________________________________________________
method CometCompo_evolution_CFC Add_L_outputs {L} {set this(L_outputs) [concat $this(L_outputs) $L]}
method CometCompo_evolution_CFC Add_L_inputs  {L} {set this(L_inputs)  [concat $this(L_inputs)  $L]}


#___________________________________________________________________________________________________________________________________________
method CometCompo_evolution_CFC Process {} {incr this(nb_process)}
method CometCompo_evolution_CFC set_Input_data {name values} {}
method CometCompo_evolution_CFC set_Output_data {name values} {}
method CometCompo_evolution_CFC get_Input_data {name} {}
method CometCompo_evolution_CFC get_Output_data {name} {}

#___________________________________________________________________________________________________________________________________________
method CometCompo_evolution_CFC Reset {} {}

#___________________________________________________________________________________________________________________________________________
proc P_L_methodes_get_CometCompo_evolution {} {return [list {get_has_processed { }} \
     {get_L_outputs { }}       \
     {get_L_inputs { }}        \
     {get_L_param { }}         \
     {get_Core { }}            \
     {get_Ext_input { }}       \
     {get_Ext_output { }}      \
     {get_Input_data {name}}   \
     {get_Output_data {name}}  \
	 {get_can_process {}}      \
	 {get_has_error {}}        \
	 {get_nb_max_process {}}   \
	 {get_nb_process {}}       \
	 {get_param_data {name}}   \
	 {get_serialization {}}    \
	 {append_to_serialization {v}}    \
	 {Reset_serialization {}}  \
	 {get_C_Magellan {}} \
	 {get_comet_root_to_be_transformed {}} \
	 ]}
	
#___________________________________________________________________________________________________________________________________________
proc P_L_methodes_set_CometCompo_evolution {} {return [list {set_has_processed {v}} \
     {Process {}} \
     {set_L_outputs {v}}              \
     {Add_L_outputs {v}}              \
     {Sub_L_outputs {v}}              \
     {set_L_inputs {v}}               \
     {Add_L_inputs {v}}               \
     {Sub_L_inputs {v}}               \
     {set_L_param {v}}                \
     {Add_L_param {v}}                \
     {Sub_L_param {v}}                \
     {set_Core {v}}                   \
     {set_Ext_input {v}}              \
     {set_Ext_output {v}}             \
     {set_Input_data {name values}}   \
     {set_Output_data {name values}}  \
	 {set_can_process {v}}            \
	 {set_has_error {v}}              \
	 {set_has_processed {v}}          \
	 {set_nb_max_process {v}}         \
	 {set_nb_process {v}}             \
	 {Reset {}}                       \
	 {set_param_data {name value}}    \
	 {load_core {file_name}}          \
	 {Serialize {prefix}}             \
	 {set_C_Magellan {v}}			  \
     ]}

#___________________________________________________________________________________________________________________________________________
method CometCompo_evolution_CFC get_serialization {} {return $this(serialization)}

#___________________________________________________________________________________________________________________________________________
method CometCompo_evolution_CFC Reset_serialization {} {
 set this(serialization) ""
}

#___________________________________________________________________________________________________________________________________________
method CometCompo_evolution_CFC append_to_serialization {v} {
 append this(serialization) $v
}

#___________________________________________________________________________________________________________________________________________
method CometCompo_evolution_CFC Serialize {prefix} {
   foreach i $this(L_inputs) {
     lassign $i id cardN card oldValues
     append this(serialization) "  <input id=\"$id\" cardN=\"$cardN\"/>\n"
    }

   foreach o $this(L_outputs) {
     lassign $o id cardN card oldValues
     append this(serialization) "  <output id=\"$id\" cardN=\"$cardN\"/>\n"
    }
	
   foreach p $this(L_param) {
     lassign $p id val
	 set val [string map [list "\"" "\\\""  "&" "&amp\;"  "\"" "&quot;"  "<" "&lt;"  ">" "&gt;"  "'" "&apos;"] $val]
     append this(serialization) "  <param id=\"$id\" val=\"$val\"/>\n"
    }
	
}

#___________________________________________________________________________________________________________________________________________
method CometCompo_evolution_CFC load_core {file_name} {
 if {![file exists $file_name]} {puts "File $file_name does not exist"; return}
 set this(core_f_name) $file_name
 set f [open $file_name]
   this set_Core [read $f]
 close $f
}

#___________________________________________________________________________________________________________________________________________
method CometCompo_evolution_CFC get_param_data {name} {
 foreach p $this(L_param) {
   if {[lindex $p 0] == $name} {return [lindex $p 1]}
  }
 return ""
}

#___________________________________________________________________________________________________________________________________________
method CometCompo_evolution_CFC set_param_data {name value} {
 set pos 0
 foreach p $this(L_param) {
   if {[lindex $p 0] == $name} {
     set this(L_param) [lreplace $this(L_param) $pos $pos [list $name $value]]
	 return
    }
   incr pos
  }
  
 lappend this(L_param) [list $name $value]
 
}

#___________________________________________________________________________________________________________________________________________
Inject_code CometCompo_evolution_CFC get_Input_data {
    set l_input [this get_L_inputs] 
    #get first input that match the name
    set pos [lsearch $l_input $name*]
    #cardinality ?
    lassign [lindex $l_input $pos] id cardN card oldValues
    #if {$card == [llength $oldValues]} {
	return $oldValues
    #} 
	#return {}
    
}  {}

#___________________________________________________________________________________________________________________________________________
Inject_code CometCompo_evolution_CFC set_Input_data {
    set l_input [this get_L_inputs] 
    #get first input that match the name
    set pos [lsearch $l_input $name*]
    #cardinality ?
    lassign [lindex $l_input $pos] id cardN card oldValues
    if {$pos >= 0} {
	  set l_input [lreplace $l_input $pos $pos [list $id $cardN $card $values] ]
#	  puts -nonewline "In $objName set_input_data $name:\n  Scores = {"
#	  foreach v $values {
#	    puts -nonewline "[lindex $v 0] "
#	   }
#	  puts "}"
#     }

    this set_L_inputs $l_input

} {	
#check if all input are filled. i.e ready to process.
	#this set_can_process 1
	#foreach input [this get_L_inputs] {
	#    lassign $input name cardN card values 
	#    if {[llength $values] != $card} {
	#	   this set_can_process 0
	#     }
	# }
}
 
#___________________________________________________________________________________________________________________________________________
Inject_code CometCompo_evolution_CFC get_Output_data {
    set l_output [this get_L_outputs] 
    #get first input that match the name
    set pos [lsearch $l_output $name*]
    #cardinality ?
    lassign [lindex $l_output $pos] id cardN card oldValues
    #if {$card == [llength $oldValues]} {
	return $oldValues
    #} 
	#return {}
} {}

#___________________________________________________________________________________________________________________________________________
Inject_code CometCompo_evolution_CFC set_Output_data {
    set l_output [this get_L_outputs] 
    #get first input that match the name
    set pos [lsearch $l_output $name*]
    #cardinality ?
    lassign [lindex $l_output $pos] id cardN card oldValues
    if {$pos >= 0} {
	  set l_output [lreplace $l_output $pos $pos [list $id $cardN $card $values] ]
     }
	
	this set_L_outputs $l_output
	
} {
	#check if all output have been processed.
	#this set_has_processed 1
	#foreach output [this get_L_outputs] {
	#    lassign $output name cardN card values 
	#    if {[llength $values] != $card} {
	#	  this set_has_processed 0
	#     }
	# }
} 

